package com.pos.clothes.controller;

import com.github.pagehelper.PageInfo;
import com.pos.clothes.pojo.Category;
import com.pos.clothes.pojo.Color;
import com.pos.clothes.pojo.Size;
import com.pos.clothes.service.ColorService;
import com.pos.clothes.util.JsonData;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequiresPermissions("3")
@RequestMapping("/Color")
public class ColorController  {
    @Autowired
    public ColorService service;

    @Autowired
    public Color color;

//    @RequestMapping("/getColor")
//    @ResponseBody
//    public PageInfo getColor(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) throws Exception {
//        PageInfo res = service.getColor(pageNum,pageSize);
//        return res;
//    }
    @RequestMapping("/getColor")
    @ResponseBody
    public List<Color> getColor(){

        List<Color> colors = service.getColor();
        return colors;
    }

    @RequestMapping(value = "/deleteColor")
    @ResponseBody
    public Object deleteColor(@RequestParam("idcolor") Integer idcolor){
        try {
            Integer res = service.deleteColor(idcolor);
            if (res != 0) {
                return JsonData.buildSuccess(res, "删除成功");
            } else {
                return JsonData.buildError("删除失败");
            }
        }
        catch(Exception e) {
            return JsonData.buildError("删除失败！请先删除存在该颜色的规格商品！！");
        }
    }
    @RequestMapping(value = "/insertColor")
    @ResponseBody
    public Object insertColor(@RequestParam("color") String color){
        if(color==""){
            return JsonData.buildError("颜色名称不能为空");
        }
        List<Color> colors = service.searchColor(color);
        if(!colors.isEmpty()){
            return JsonData.buildError("颜色名称已经存在");
        }
        this.color.setColor(color);
        service.insertColor(this.color);
        return JsonData.buildSuccess(this.color.getIdcolor(),"ok");

    }

    @RequestMapping(value = "/updateColor")
    @ResponseBody
    public Object updateNorms(@RequestParam("idcolor") int idcolor,@RequestParam("color") String color){
        if(color==""){
            return JsonData.buildError("颜色名不能为空");
        }

        List<Color> colors = service.searchColor(color);
        if(!colors.isEmpty()) {
            if(colors.get(0).getIdcolor()!=idcolor ) {
                return JsonData.buildError("颜色已存在");

            }

        }
        this.color.setIdcolor(idcolor);
        this.color.setColor(color);
        service.updateColor(this.color);
        return JsonData.buildSuccess(this.color.getIdcolor(),"ok");
    }

}
